0

Untitled.ipynb

No Headings

The table of contents shows headings in notebooks and supported files.

Skip to Main
Jupyter

Untitled

Last Checkpoint: 1 minute ago
  • File
  • Edit
  • View
  • Run
  • Kernel
  • Settings
  • Help
JupyterLab
Python 3 (ipykernel)
Kernel status: Idle
    [1]:
    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
    from sklearn.preprocessing import StandardScaler, PolynomialFeatures
    from sklearn.decomposition import PCA
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.feature_selection import RFE
    from sklearn.linear_model import LogisticRegression
    from sklearn.svm import SVC
    [3]:
    age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal target
    0 52 1 0 125 212 0 1 168 0 1.0 2 2 3 0
    1 53 1 0 140 203 1 0 155 1 3.1 0 0 3 0
    2 70 1 0 145 174 0 1 125 1 2.6 0 0 3 0
    3 61 1 0 148 203 0 1 161 0 0.0 2 1 3 0
    4 62 0 0 138 294 1 1 106 0 1.9 1 3 2 0
       age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  slope  \
    0   52    1   0       125   212    0        1      168      0      1.0      2   
    1   53    1   0       140   203    1        0      155      1      3.1      0   
    2   70    1   0       145   174    0        1      125      1      2.6      0   
    3   61    1   0       148   203    0        1      161      0      0.0      2   
    4   62    0   0       138   294    1        1      106      0      1.9      1   
    
       ca  thal  target  
    0   2     3       0  
    1   0     3       0  
    2   0     3       0  
    3   1     3       0  
    4   3     2       0  
    
    [5]:
    age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal target
    1020 59 1 1 140 221 0 1 164 1 0.0 2 0 2 1
    1021 60 1 0 125 258 0 0 141 1 2.8 1 1 3 0
    1022 47 1 0 110 275 0 0 118 1 1.0 1 1 2 0
    1023 50 0 0 110 254 0 0 159 0 0.0 2 0 2 1
    1024 54 1 0 120 188 0 1 113 0 1.4 1 1 3 0
          age  sex  cp  trestbps  chol  fbs  restecg  thalach  exang  oldpeak  \
    1020   59    1   1       140   221    0        1      164      1      0.0   
    1021   60    1   0       125   258    0        0      141      1      2.8   
    1022   47    1   0       110   275    0        0      118      1      1.0   
    1023   50    0   0       110   254    0        0      159      0      0.0   
    1024   54    1   0       120   188    0        1      113      0      1.4   
    
          slope  ca  thal  target  
    1020      2   0     2       1  
    1021      1   1     3       0  
    1022      1   1     2       0  
    1023      2   0     2       1  
    1024      1   1     3       0  
    
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 1025 entries, 0 to 1024
    Data columns (total 14 columns):
     #   Column    Non-Null Count  Dtype  
    ---  ------    --------------  -----  
     0   age       1025 non-null   int64  
     1   sex       1025 non-null   int64  
     2   cp        1025 non-null   int64  
     3   trestbps  1025 non-null   int64  
     4   chol      1025 non-null   int64  
     5   fbs       1025 non-null   int64  
     6   restecg   1025 non-null   int64  
     7   thalach   1025 non-null   int64  
     8   exang     1025 non-null   int64  
     9   oldpeak   1025 non-null   float64
     10  slope     1025 non-null   int64  
     11  ca        1025 non-null   int64  
     12  thal      1025 non-null   int64  
     13  target    1025 non-null   int64  
    dtypes: float64(1), int64(13)
    memory usage: 112.2 KB
    None
    
                   age          sex           cp     trestbps        chol  \
    count  1025.000000  1025.000000  1025.000000  1025.000000  1025.00000   
    mean     54.434146     0.695610     0.942439   131.611707   246.00000   
    std       9.072290     0.460373     1.029641    17.516718    51.59251   
    min      29.000000     0.000000     0.000000    94.000000   126.00000   
    25%      48.000000     0.000000     0.000000   120.000000   211.00000   
    50%      56.000000     1.000000     1.000000   130.000000   240.00000   
    75%      61.000000     1.000000     2.000000   140.000000   275.00000   
    max      77.000000     1.000000     3.000000   200.000000   564.00000   
    
                   fbs      restecg      thalach        exang      oldpeak  \
    count  1025.000000  1025.000000  1025.000000  1025.000000  1025.000000   
    mean      0.149268     0.529756   149.114146     0.336585     1.071512   
    std       0.356527     0.527878    23.005724     0.472772     1.175053   
    min       0.000000     0.000000    71.000000     0.000000     0.000000   
    25%       0.000000     0.000000   132.000000     0.000000     0.000000   
    50%       0.000000     1.000000   152.000000     0.000000     0.800000   
    75%       0.000000     1.000000   166.000000     1.000000     1.800000   
    max       1.000000     2.000000   202.000000     1.000000     6.200000   
    
                 slope           ca         thal       target  
    count  1025.000000  1025.000000  1025.000000  1025.000000  
    mean      1.385366     0.754146     2.323902     0.513171  
    std       0.617755     1.030798     0.620660     0.500070  
    min       0.000000     0.000000     0.000000     0.000000  
    25%       1.000000     0.000000     2.000000     0.000000  
    50%       1.000000     0.000000     2.000000     1.000000  
    75%       2.000000     1.000000     3.000000     1.000000  
    max       2.000000     4.000000     3.000000     1.000000  
    
    Index(['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach',
           'exang', 'oldpeak', 'slope', 'ca', 'thal', 'target'],
          dtype='object')
    
    [19]:
    RandomForestClassifier(random_state=42)
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    RandomForestClassifier(random_state=42)
    [24]:
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42),
        n_features_to_select=10)
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42),
        n_features_to_select=10)
    LogisticRegression(max_iter=5000, random_state=42)
    LogisticRegression(max_iter=5000, random_state=42)
    [26]:
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42),
        n_features_to_select=10)
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42),
        n_features_to_select=10)
    LogisticRegression(max_iter=5000, random_state=42)
    LogisticRegression(max_iter=5000, random_state=42)
    [27]:
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42, solver='saga'),
        n_features_to_select=10)
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    RFE(estimator=LogisticRegression(max_iter=5000, random_state=42, solver='saga'),
        n_features_to_select=10)
    LogisticRegression(max_iter=5000, random_state=42, solver='saga')
    LogisticRegression(max_iter=5000, random_state=42, solver='saga')
    Selected Features: Index(['age', 'sex', 'cp', 'exang', 'oldpeak', 'ca', 'thal',
           'trestbps_chol_interaction', 'thalach_age_ratio', 'age_group_55-70'],
          dtype='object')
    
    Logistic Regression CV Accuracy: 0.8517
    
    Best Random Forest Parameters: {'max_depth': 10, 'min_samples_split': 2, 'n_estimators': 100}
    
    Random Forest CV Accuracy: 0.9941
    
    SVM CV Accuracy: 0.8507
    
    Best Model: Random Forest with Accuracy: 0.9941
    
    [38]:
    RandomForestClassifier(random_state=42)
    In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
    On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
    RandomForestClassifier(random_state=42)
    [50]:
    import seaborn as sns

    # Create DataFrame for plotting
    df_plot = pd.DataFrame(X_scaled, columns=X.columns)
    df_plot['target'] = y

    # Plot
    sns.pairplot(df_plot, hue='target', diag_kind='kde')
    plt.title('Pair Plot of Features')
    plt.show()
    [51]:
    # Add engineered features to the DataFrame
    df_plot['age_chol_interaction'] = X_scaled[:, X.columns.get_loc('age')] * X_scaled[:, X.columns.get_loc('chol')]
    df_plot['chol_age_ratio'] = X_scaled[:, X.columns.get_loc('chol')] / (X_scaled[:, X.columns.get_loc('age')] + 1e-10)
    # Plot histograms
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 2, 1)
    sns.histplot(df_plot['age_chol_interaction'], kde=True)
    plt.title('Age-Cholesterol Interaction')

    plt.subplot(1, 2, 2)
    sns.histplot(df_plot['chol_age_ratio'], kde=True)
    plt.title('Cholesterol to Age Ratio')

    plt.show()
    [ ]:

    -

    Variables

    Callstack

      Breakpoints

      Source

      9
      1

      Kernel Sources

      Common Tools
      No metadata.
      Advanced Tools
      No metadata.
      Alt+[
      Alt+]
      Alt+End
      • Console
      • Change Kernel…
      • Clear Console Cells
      • Close and Shut Down…
      • Insert Line Break
      • Interrupt Kernel
      • New Console
      • Restart Kernel…
      • Run Cell (forced)
      • Run Cell (unforced)
      • Show All Kernel Activity
      • Debugger
      • Breakpoints on exception
      • Evaluate Code
        Evaluate Code
      • Next
        Next
        F10
      • Pause
        Pause
        F9
      • Step In
        Step In
        F11
      • Step Out
        Step Out
        Shift+F11
      • Terminate
        Terminate
        Shift+F9
      • Display Languages
      • English
        English
      • File Operations
      • Autosave Documents
      • Download
        Download the file to your computer
      • Reload Notebook from Disk
        Reload contents from disk
      • Revert Notebook to Checkpoint…
        Revert contents to previous checkpoint
      • Save Notebook
        Save and create checkpoint
        Ctrl+S
      • Save Notebook As…
        Save with new path
        Ctrl+Shift+S
      • Trust HTML File
        Whether the HTML file is trusted. Trusting the file allows scripts to run in it, which may result in security risks. Only enable for files you trust.
      • Help
      • About Jupyter Notebook
      • Jupyter Reference
      • JupyterLab FAQ
      • JupyterLab Reference
      • Launch Jupyter Notebook File Browser
      • Markdown Reference
      • Show Keyboard Shortcuts…
        Show relevant keyboard shortcuts for the current active widget
        Ctrl+Shift+H
      • Image Viewer
      • Flip image horizontally
        H
      • Flip image vertically
        V
      • Invert Colors
        I
      • Reset Image
        0
      • Rotate Clockwise
        ]
      • Rotate Counterclockwise
        [
      • Zoom In
        =
      • Zoom Out
        -
      • Kernel Operations
      • Shut Down All Kernels…
      • Main Area
      • Close All Other Tabs
      • Close Tab
        Alt+W
      • Close Tabs to Right
      • End Search
        Esc
      • Find Next
        Ctrl+G
      • Find Previous
        Ctrl+Shift+G
      • Find…
        Ctrl+F
      • Log Out
        Log out of Jupyter Notebook
      • Search in Selection
        Alt+L
      • Shut Down
        Shut down Jupyter Notebook
      • Mode
      • Toggle Zen Mode
      • Notebook Cell Operations
      • Change to Code Cell Type
        Y
      • Change to Heading 1
        1
      • Change to Heading 2
        2
      • Change to Heading 3
        3
      • Change to Heading 4
        4
      • Change to Heading 5
        5
      • Change to Heading 6
        6
      • Change to Markdown Cell Type
        M
      • Change to Raw Cell Type
        R
      • Clear Cell Output
        Clear outputs for the selected cells
      • Collapse All Code
      • Collapse All Outputs
      • Collapse Selected Code
      • Collapse Selected Outputs
      • Copy Cell
        Copy this cell
        C
      • Cut Cell
        Cut this cell
        X
      • Delete Cell
        Delete this cell
        D, D
      • Disable Scrolling for Outputs
      • Enable Scrolling for Outputs
      • Expand All Code
      • Expand All Outputs
      • Expand Selected Code
      • Expand Selected Outputs
      • Extend Selection Above
        Shift+K
      • Extend Selection Below
        Shift+J
      • Extend Selection to Bottom
        Shift+End
      • Extend Selection to Top
        Shift+Home
      • Insert Cell Above
        Insert a cell above
        A
      • Insert Cell Below
        Insert a cell below
        B
      • Insert Heading Above Current Heading
        Shift+A
      • Insert Heading Below Current Heading
        Shift+B
      • Merge Cell Above
        Ctrl+Backspace
      • Merge Cell Below
        Ctrl+Shift+M
      • Merge Selected Cells
        Shift+M
      • Move Cell Down
        Move this cell down
        Ctrl+Shift+Down
      • Move Cell Up
        Move this cell up
        Ctrl+Shift+Up
      • Paste Cell Above
        Paste this cell from the clipboard
      • Paste Cell and Replace
      • Paste Cell Below
        Paste this cell from the clipboard
        V
      • Redo Cell Operation
        Shift+Z
      • Render Side-by-Side
        Shift+R
      • Run Selected Cell
        Run this cell and advance
        Shift+Enter
      • Run Selected Cell and Do not Advance
        Ctrl+Enter
      • Run Selected Cell and Insert Below
        Alt+Enter
      • Run Selected Text or Current Line in Console
      • Select Cell Above
        K
      • Select Cell Below
        J
      • Select Heading Above or Collapse Heading
        Left
      • Select Heading Below or Expand Heading
        Right
      • Set side-by-side ratio
      • Split Cell
        Ctrl+Shift+-
      • Undo Cell Operation
        Z
      • Notebook Operations
      • Access Next Kernel History Entry
        Alt+Down
      • Access Previous Kernel History Entry
        Alt+Up
      • Change Kernel…
      • Clear Outputs of All Cells
        Clear all outputs of all cells
      • Close and Shut Down Notebook…
      • Collapse All Headings
        Ctrl+Shift+Left
      • Deselect All Cells
      • Edit Notebook Metadata
      • Enter Command Mode
        Ctrl+M
      • Enter Edit Mode
        Enter
      • Expand All Headings
        Ctrl+Shift+Right
      • Interrupt Kernel
        Interrupt the kernel
      • New Console for Notebook
      • New Notebook
        Create a new notebook
      • Reconnect to Kernel
      • Render All Markdown Cells
      • Restart Kernel and Clear Outputs of All Cells…
        Restart the kernel and clear all outputs of all cells
      • Restart Kernel and Debug…
        Restart Kernel and Debug…
      • Restart Kernel and Run All Cells…
        Restart the kernel and run all cells
      • Restart Kernel and Run up to Selected Cell…
      • Restart Kernel…
        Restart the kernel
      • Run All Above Selected Cell
      • Run All Cells
        Run all cells
      • Run Selected Cell and All Below
      • Save and Export Notebook: Asciidoc
      • Save and Export Notebook: Executable Script
      • Save and Export Notebook: HTML
      • Save and Export Notebook: LaTeX
      • Save and Export Notebook: Markdown
      • Save and Export Notebook: PDF
      • Save and Export Notebook: Qtpdf
      • Save and Export Notebook: Qtpng
      • Save and Export Notebook: ReStructured Text
      • Save and Export Notebook: Reveal.js Slides
      • Save and Export Notebook: Webpdf
      • Select All Cells
        Ctrl+A
      • Show Line Numbers
      • Toggle Collapse Notebook Heading
      • Trust Notebook
      • Other
      • Open in JupyterLab
        JupyterLab
      • Plugin Manager
      • Advanced Plugin Manager
      • Terminal
      • Decrease Terminal Font Size
      • Increase Terminal Font Size
      • New Terminal
        Start a new terminal session
      • Refresh Terminal
        Refresh the current terminal session
      • Use Terminal Theme: Dark
        Set the terminal theme
      • Use Terminal Theme: Inherit
        Set the terminal theme
      • Use Terminal Theme: Light
        Set the terminal theme
      • Text Editor
      • Decrease Font Size
      • Increase Font Size
      • New Markdown File
        Create a new markdown file
      • New Python File
        Create a new Python file
      • New Text File
        Create a new text file
      • Spaces: 1
      • Spaces: 2
      • Spaces: 4
      • Spaces: 4
      • Spaces: 8
      • Theme
      • Decrease Code Font Size
      • Decrease Content Font Size
      • Decrease UI Font Size
      • Increase Code Font Size
      • Increase Content Font Size
      • Increase UI Font Size
      • Set Preferred Dark Theme: JupyterLab Dark
      • Set Preferred Dark Theme: JupyterLab Dark High Contrast
      • Set Preferred Dark Theme: JupyterLab Light
      • Set Preferred Light Theme: JupyterLab Dark
      • Set Preferred Light Theme: JupyterLab Dark High Contrast
      • Set Preferred Light Theme: JupyterLab Light
      • Synchronize Styling Theme with System Settings
      • Theme Scrollbars
      • Use Theme: JupyterLab Dark
      • Use Theme: JupyterLab Dark High Contrast
      • Use Theme: JupyterLab Light
      • View
      • File Browser
      • Open JupyterLab
      • Show Debugger
        Show Show Debugger in the right sidebar
      • Show Header
      • Show Notebook Tools
        Show Show Notebook Tools in the right sidebar
      • Show Table of Contents
        Show Show Table of Contents in the left sidebar